package com.hotel.customersystem.controller;


import com.common.status.ResultMsg;
import com.common.status.StatusCode;
import com.common.utils.ResultUtils;
import com.common.utils.ResultVo;
import com.hotel.customersystem.pojo.CustomerCollection;
import com.hotel.customersystem.pojo.Pianhao;
import com.hotel.customersystem.service.CustomerCollectionService;
import com.hotel.customersystem.service.PianhaoService;
import com.hotel.hotelsystem.pojo.HotelList;
import com.hotel.hotelsystem.service.HotelIndexRankingListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CustomerCollection)表控制层
 *
 * @author makejava
 * @since 2022-09-30 16:06:38
 */
@Api(value = "CustomerUserController",tags = {"个人中心收藏模块"})
@RestController
public class CustomerCollectionController {
    /**
     * 服务对象
     */
    @Resource
    private CustomerCollectionService customerCollectionService;

    @Resource
    private PianhaoService pianhaoService;

    // 热门酒店
    @Resource
    private HotelIndexRankingListService hotelIndexRankingListService;

    @ApiOperation(value = "个人中心收藏列表")
    @GetMapping("/getCollectionList/{userId}")
    public ResultVo<List<CustomerCollection>> getCollectionList(@PathVariable("userId") Integer userId) {
        List<CustomerCollection> list = customerCollectionService.getCollectList(userId);
        if (list.size()!=0) return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,list);
        else return ResultUtils.error(ResultMsg.DATA_NULL, StatusCode.SYSTEM_ERROR_CODE,list);
    }

    @ApiOperation(value = "个人中心收藏删除")
    @PutMapping("/collection/deleteCollection/{collectionId}")
    public ResultVo<List<CustomerCollection>> deleteCollection(@PathVariable("collectionId") Integer collectionId) {
        Boolean flag = customerCollectionService.deleteCollection(collectionId);
        if (flag) return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,flag);
        else return ResultUtils.error(ResultMsg.DATA_NULL, StatusCode.SYSTEM_ERROR_CODE,flag);
    }

    @ApiOperation(value = "个人中心收藏多选删除")
    @PutMapping("/collection/deleteCheckedCollection")
    public ResultVo<List<CustomerCollection>> deleteCheckedCollection(@RequestBody List<CustomerCollection> list) {
        Boolean flag = customerCollectionService.deleteListCollection(list);
        if (flag) return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,flag);
        else return ResultUtils.error(ResultMsg.DATA_NULL, StatusCode.SYSTEM_ERROR_CODE,flag);
    }

    /**
     * 用户取消收藏(删除)
     * @param userId
     * @param hotelId
     * @return
     */
    @ApiOperation(value = "用户取消收藏(删除)")
    @GetMapping("/collection/redis/removeUserId/{userId}/{hotelId}")
    public Boolean removeCollection(@PathVariable("userId") String userId,@PathVariable("hotelId") String hotelId){
        System.out.println("收藏1...");
        return customerCollectionService.redisRemoveCollection(userId,hotelId);
    }

    /**
     * 用户进行收藏(添加)
     * @param userId
     * @param hotelId
     * @return
     */
    @ApiOperation(value = "用户进行收藏(添加)")
    @GetMapping("/collection/redis/insertUserId/{userId}/{hotelId}")
    public Boolean insertCollection(@PathVariable("userId") String userId,@PathVariable("hotelId") String hotelId){
        System.out.println("收藏2...");
        return customerCollectionService.redisInsertCollection(userId,hotelId);
    }

    /**
     * 判断用户是否已经收藏
     * 去redis当中进行查询判断是否存在该对象，返回boolean类型
     * 需要获得 1.当前用户id  2.查看的酒店id
     * @param userId
     */
    @ApiOperation(value = "判断用户是否已经收藏")
    @GetMapping("/collection/redis/selectById/{userId}/{hotelId}")
    public Boolean selectIsCollection(@PathVariable("userId") String userId,@PathVariable("hotelId") String hotelId){
        System.out.println("收藏3...");
        return customerCollectionService.redisSelectIsCollection(userId,hotelId);
    }


    @ApiOperation(value = "详情页推荐酒店")
    @GetMapping("/recommendHotelByHotelId")
    public ResultVo<List<HotelList>> recommendHotelByHotelId(@RequestParam Integer userId,@RequestParam Integer hotelId,@RequestParam Integer howMany) {
        List<HotelList> list = pianhaoService.getRecommentProductByProduct(userId, hotelId, howMany);
        if (list != null) return ResultUtils.success(ResultMsg.SUCCESS_MSG,StatusCode.SUCCESS_CODE,list);
        if(list == null) {
            // 传热门酒店数据
            List<HotelList> hotelLists = hotelIndexRankingListService.rankingList(howMany);
            return ResultUtils.success(ResultMsg.SUCCESS_MSG,StatusCode.SUCCESS_CODE,hotelLists);
        }
        return null;
    }

    @ApiOperation(value = "个人中心推荐酒店")
    @GetMapping("/recommendHotelByUserId")
    public ResultVo<List<Pianhao>> recommendHotelByUserId(@RequestParam Integer userId,@RequestParam Integer howMany) {
        System.out.println("--->"+userId);
        System.out.println("<---"+howMany);
//        System.out.println("userId=========="+userId+"howMany:"+howMany);
        List<HotelList> list = pianhaoService.getRecommentProductByUser(userId, howMany);
        if (list != null) return ResultUtils.success(ResultMsg.SUCCESS_MSG,StatusCode.SUCCESS_CODE,list);
        else {
            // 传热门酒店数据
            List<HotelList> hotelLists = hotelIndexRankingListService.rankingList(howMany);
            return ResultUtils.success(ResultMsg.SUCCESS_MSG,StatusCode.SUCCESS_CODE,hotelLists);
        }
    }


}

